﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zocono.WCS.Domain.EntityInfo;
using AutoMapper;
using Zocono.WCS.Domain.DomainRepository;
using Zocono.WCS.Infrastructure.Repository;
using SqlSugar;
using Zocono.WCS.Infrastructure.ApllicationDto.DtoWcsManage;
using System.Linq.Expressions;

namespace Zocono.WCS.Domain.UserManageDomain
{
    public class RoleRightInfoService : IRoleRightInfoService
    {
        private IMapper _Mapper;
        private IRoleRightInfoRepository _RoleRightInfoRepository;

        public RoleRightInfoService(IMapper map , IRoleRightInfoRepository roleRightInfoRepository)
        {
            _Mapper = map;
            _RoleRightInfoRepository = roleRightInfoRepository;
        }


        /// <summary>
        /// 添加角色权限
        /// </summary>
        /// <param name="userRightInfoDto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<bool> AddRoleRightAsync(List<RoleRightInfoDto> roleRightInfoDto)
        {
            List<RoleRightInfo> roleRightInfo = new List<RoleRightInfo>();
            _Mapper.Map(roleRightInfoDto, roleRightInfo);

            try
            {
                var res = await _RoleRightInfoRepository.AsSugarClient().Insertable(roleRightInfo).ExecuteCommandAsync();
                return res==1;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

     

       

        /// <summary>
        /// 删除角色对应的权限
        /// </summary>
        /// <param name="roleID"></param>
        /// <returns></returns>
        public async Task<bool> DeleteRoleRightAsync(Expression<Func<RoleRightInfo,bool>> func)
        {
            return await _RoleRightInfoRepository.DeleteAsync(func);        
           
        }

        /// <summary>
        /// 获取用户权限菜单ID
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public async Task<List<RoleRightInfoDto>> GetRoleRightAsync(long userID)
        {
            List<RoleRightInfoDto> roleRightInfoDto = new List<RoleRightInfoDto>();
            List<long> roleID=new List<long>();
            roleID= await _RoleRightInfoRepository.AsSugarClient().Queryable<UserRoleInfo>().Where(x => x.UserID == userID).Select(x => x.RoleID).ToListAsync();
            List<RoleRightInfo> result = await _RoleRightInfoRepository.AsSugarClient().Queryable<RoleRightInfo>().Distinct().Where(x => roleID.Contains(x.RoleID)).Select(x => x).ToListAsync();
            _Mapper.Map(result, roleRightInfoDto);

            return roleRightInfoDto;
        }

        /// <summary>
        /// 获取角色菜单ID
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public async Task<List<RoleRightInfoDto>> GetRoleMenuByIDAsync(long roleID)
        {
            List<RoleRightInfoDto> roleRightInfoDto = new List<RoleRightInfoDto>();
            var result = await _RoleRightInfoRepository.AsSugarClient().Queryable<RoleRightInfo>().Distinct().Where(x => roleID==x.RoleID).Select(x => x).ToListAsync();
            _Mapper.Map(result, roleRightInfoDto);

            return roleRightInfoDto;
        }

        //public void RollBack()
        //{
        //    throw new NotImplementedException();
        //}

        //public void Commit()
        //{
        //    _RoleRightInfoRepository.AsTenant().
        //}

        //public void Begin()
        //{
        //    throw new NotImplementedException();
        //}
    }
}
